package com.kuang.springcloud.coller;

import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @PackageName:com.kuang.springcloud.coller
 * @ClassName:DeptColler
 * @Description: 提供Restful服务
 * @Author: zhangxiaoyu
 * @date 2021/5/3 18:57
 */
@RestController
public class DeptColler {

    @Autowired
    private DeptService deptService;

    @Autowired
    private DiscoveryClient client;


    @PostMapping("/dept/add")
    public boolean addDept(Dept dept){
        return deptService.addDept(dept);
    }


    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable Long id){
        return deptService.queryById(id);
    }

    @GetMapping("/dept/list")
    public List<Dept> addDept(){
        return deptService.queryAll();
    }


    @RequestMapping("/dept/discovery")
    public Object discovery(){
        //注册进来的微服务，获取一些消息
        //获取微服务列表的清单
       List<String> services =  client.getServices();
       System.out.println("discovery=>services"+services);

       //得到一个具体的微服务信息,通过具体的微服务id Application name
       List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");

       for (ServiceInstance serviceInstance:instances){
           System.out.println(serviceInstance.getHost()+"\t"+serviceInstance.getPort()+"\t"+serviceInstance.getUri());
       }
       return this.client;
    }
}
